﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.Query.Member;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Domain.Shared.Dto.Member;
using furion.blog.Domain.Shared.Enum;
using furion.blog.Utils;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 会员操作类
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "MemberApi" })]
    [Route("api/member")]
    public class MemberApiController : IDynamicApiController
    {
        private readonly IMemberService _memberService;
        public MemberApiController(IMemberService memberService)
        {
            this._memberService = memberService;
        }


        /**********************************以下后端接口**************************************************************/

        [ActionAttribute(Name = "会员分页接口", Desc = "会员分页信息")]
        [HttpGet, Route("list")]
        public async Task<IActionResult> list(
            [FromQuery(Name = "page")] int pageIndex,
            [FromQuery(Name = "limit")] int pageSize,
            [FromQuery(Name = "searchContent")] string searchContent = "",
            [FromQuery(Name = "status")] string status = ""
            )
        {
            var ret = await this._memberService.List( new MemberListQuery { 
               PageSize = pageSize,
               PageIndex = pageIndex,
               SearchContent = searchContent,
               Status = status,
               IsDelete = YesOrNoEnum.No.ToString(),
            });
            return new OkObjectResult(ResultUtil.Success(ret));
        }



        /**********************************以下app接口**************************************************************/


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [ActionAttribute(Name = "会员登录接口", Desc = "会员登录")]
        [HttpPost, Route("login"), AllowAnonymous]
        public async Task<IActionResult> login([FromBody] MemberLoginDto dto)
        {
            var ret = await this._memberService.Login(dto);
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        [ActionAttribute(Name = "会员信息接口", Desc = "当前登录会员信息")]
        [HttpGet, Route("profile")]
        public async Task<IActionResult> profile([FromHeader(Name = "MemberId")]long memberId) {
            var ret = await this._memberService.Profile(memberId);
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        [ActionAttribute(Name = "会员信息更新接口", Desc = "更新会员个人信息")]
        [HttpPost, Route("profile/update")]
        public async Task<IActionResult> updateProfile([FromBody]MemberProfileUpdateDto dto,[FromHeader(Name = "MemberId")] long memberId) {
            dto.MemberId = memberId;
            await this._memberService.UpdateProfile(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "会员绑定手机接口", Desc = "会员绑定手机号码")]
        [HttpPost, Route("phone/bind")]
        public async Task<IActionResult> bindPhone([FromBody]MemberPhoneBindDto dto,[FromHeader(Name = "MemberId")]long memberId) {
            dto.MemberId = memberId;
            var ret = await this._memberService.BindPhone(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "更新会员位置信息接口", Desc = "更新会员位置信息")]
        [HttpPost, Route("location/update")]
        public async Task<IActionResult> updateLocation([FromBody]MemberLocationDto dto,[FromHeader(Name = "MemberId")]long memberId) {
            dto.MemberId = memberId;
            await this._memberService.UpdateLocation(dto);
            return new OkObjectResult(ResultUtil.Success(true));
        }

     

    }
}
